//
//  XMGNavigationController.m
//  BuDeJie19
//
///  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//
#import "XMGNavigationController.h"
#import "XMGNavigationBar.h"
#import "XMGBackView.h"

@interface XMGNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation XMGNavigationController

/*
 打印self.interactivePopGestureRecognizer
 
 <UIScreenEdgePanGestureRecognizer: 0x7ff820496f20; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7ff820489de0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7ff8204969b0>)>>
 
    target = <_UINavigationInteractiveTransition 0x7ff8204969b0>
 
    action = handleNavigationTransition:
 
    滑动返回功能
 
 */

/*
 self.interactivePopGestureRecognizer.delegate
 
 <_UINavigationInteractiveTransition: 0x7ffe135161b0>
 */

/*
    全屏滑动返回
 
    为什么系统的手势不能全屏?
 
 */



/*
    假死状态：程序还在运行，界面死了
 
    根控制器能触发滑动手势
 
 */



/*
    滑动返回功能：导航控制器iOS7后自带
 
    恢复滑动返回功能：
 
    分析：覆盖掉系统返回按钮，滑动返回功能就失效
 
    需求：既要覆盖系统返回按钮，也要有滑动返回功能.
 
    为什么没有，谁导致滑动返回功能没有的。当覆盖掉系统返回按钮，系统额外做了一些事情，导致
    滑动返回功能失效。
 
    系统额外做了什么事情，导致滑动返回功能失效？
 
    滑动返回功能，肯定需要手势? 猜测interactivePopGestureRecognizer，就有滑动返回功能
 
    验证滑动返回功能 跟interactivePopGestureRecognizer有关系
 
    让interactivePopGestureRecognizer手势失效，通过代理
 
    当覆盖掉系统返回按钮，系统额外做了一些事情，导致滑动返回功能失效
    系统额外做了一些事情 => interactivePopGestureRecognizer的代理做了事情
 
    interactivePopGestureRecognizer的代理 就是 让滑动返回失效
 */

/*
 UITabBarItem:设置UITabBar上按钮内容
 
 UINavigationItem:设置导航条哪边显示内容
 
 UIBarButtonItem:决定导航条上按钮具体内容
 */

+ (void)load{
    
    /*
        appearance:获取整个应用程序下所有东西
        appearanceWhenContainedIn:获取哪个类下东西
     
        iOS7:appearance,修改短信界面，导致联系人黑屏
     */

    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    // 设置导航条标题字体 => 导航条
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    bar.titleTextAttributes = attr;
    
    // 设置导航条背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}



#pragma mark - UIGestureRecognizerDelegate
// 控制手势是否触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count > 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 不要系统手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    id target = self.interactivePopGestureRecognizer.delegate;
    
    // 创建Pan手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
    // 设置系统滑动手势代理为自己
//    self.interactivePopGestureRecognizer.delegate = self;
    
    // 清空系统手势代理，不让它把滑动返回失效
//    self.interactivePopGestureRecognizer.delegate = self;
    
//    NSLog(@"%@",self.interactivePopGestureRecognizer.delegate);
    
    // 创建自己导航条
    XMGNavigationBar *bar = [[XMGNavigationBar alloc] initWithFrame:self.navigationBar.frame];
    
    [self setValue:bar forKey:@"navigationBar"];
    
    
//    self.navigationBar = bar;
    
    // Do any additional setup after loading the view.
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 只有非根控制器的时候才需要设置返回按钮
    if (self.childViewControllers.count > 0) { // 非根控制器
        
        // 设置返回按钮
        // 设置导航条返回按钮 获取到当前控制器的navgationItem的left
        XMGBackView *backView = [XMGBackView backViewWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(back) title:@"返回"];
        
        // 设置返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
        
        // 一定要在Push之前设置
        viewController.hidesBottomBarWhenPushed = YES;
        
        
    }
    
    
    // 真正push
    [super pushViewController:viewController animated:animated];
  
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

@end
